﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DumplicateFuntionDemo
{
    /// <summary>
    /// ArrowButton.xaml 的交互逻辑
    /// </summary>
    public partial class ArrowButton : UserControl,INotifyPropertyChanged
    {
        public ArrowButton()
        {
            InitializeComponent();
            Loaded += (s, e) => UpdateLinePositions(); // 确保初始加载时更新
            SizeChanged += ArrowButton_SizeChanged;
        }

        // 依赖属性定义
        public static readonly DependencyProperty Number1Property =
            DependencyProperty.Register("Number1", typeof(string), typeof(ArrowButton), new PropertyMetadata("1"));
        public static readonly DependencyProperty Number2Property =
            DependencyProperty.Register("Number2", typeof(string), typeof(ArrowButton), new PropertyMetadata("2"));
        public static readonly DependencyProperty Number3Property =
            DependencyProperty.Register("Number3", typeof(string), typeof(ArrowButton), new PropertyMetadata("3"));
        public static readonly DependencyProperty Number4Property =
            DependencyProperty.Register("Number4", typeof(string), typeof(ArrowButton), new PropertyMetadata("4"));
        // ...其他Number属性定义保持不变...

        public static readonly DependencyProperty LineOrientationProperty =
            DependencyProperty.Register("LineOrientation", typeof(string), typeof(ArrowButton), new PropertyMetadata("Vertical"));

        public static readonly DependencyProperty DashedLineDirectionProperty =
            DependencyProperty.Register("DashedLineDirection", typeof(string), typeof(ArrowButton), new PropertyMetadata("LeftToRight"));

        // 动态坐标依赖属性
        public static readonly DependencyProperty LeftLineStartProperty =
            DependencyProperty.Register("LeftLineStart", typeof(Point), typeof(ArrowButton), new PropertyMetadata(new Point(0, 0)));
        public static readonly DependencyProperty LeftLineEndProperty =
            DependencyProperty.Register("LeftLineEnd", typeof(Point), typeof(ArrowButton), new PropertyMetadata(new Point(0, 0)));
        public static readonly DependencyProperty RightLineStartProperty =
            DependencyProperty.Register("RightLineStart", typeof(Point), typeof(ArrowButton), new PropertyMetadata(new Point(0, 0)));
        public static readonly DependencyProperty RightLineEndProperty =
            DependencyProperty.Register("RightLineEnd", typeof(Point), typeof(ArrowButton), new PropertyMetadata(new Point(0, 0)));

        // 虚线和箭头改为依赖属性
        public static readonly DependencyProperty DashedLineStartProperty =
            DependencyProperty.Register("DashedLineStart", typeof(Point), typeof(ArrowButton), new PropertyMetadata(new Point(0, 0)));
        public static readonly DependencyProperty DashedLineEndProperty =
            DependencyProperty.Register("DashedLineEnd", typeof(Point), typeof(ArrowButton), new PropertyMetadata(new Point(0, 0)));
        public static readonly DependencyProperty ArrowheadPointsProperty =
            DependencyProperty.Register("ArrowheadPoints", typeof(string), typeof(ArrowButton), new PropertyMetadata(""));

        public string Number1
        {
            get => (string)GetValue(Number1Property);
            set => SetValue(Number1Property, value);
        }
        public string Number2
        {
            get => (string)GetValue(Number2Property);
            set => SetValue(Number2Property, value);
        }
        public string Number3
        {
            get => (string)GetValue(Number3Property);
            set => SetValue(Number3Property, value);
        }
        public string Number4
        {
            get => (string)GetValue(Number4Property);
            set => SetValue(Number4Property, value);
        }
        public string LineOrientation
        {
            get => (string)GetValue(LineOrientationProperty);
            set => SetValue(LineOrientationProperty, value);
        }
        public string DashedLineDirection
        {
            get => (string)GetValue(DashedLineDirectionProperty);
            set => SetValue(DashedLineDirectionProperty, value);
        }
        public Point LeftLineStart
        {
            get => (Point)GetValue(LeftLineStartProperty);
            set => SetValue(LeftLineStartProperty, value);
        }

        public Point LeftLineEnd
        {
            get => (Point)GetValue(LeftLineEndProperty);
            set => SetValue(LeftLineEndProperty, value);
        }

        public Point RightLineStart
        {
            get => (Point)GetValue(RightLineStartProperty);
            set => SetValue(RightLineStartProperty, value);
        }

        public Point RightLineEnd
        {
            get => (Point)GetValue(RightLineEndProperty);
            set => SetValue(RightLineEndProperty, value);
        }

        public Point DashedLineStart
        {
            get => (Point)GetValue(DashedLineStartProperty);
            set => SetValue(DashedLineStartProperty, value);
        }

        public Point DashedLineEnd
        {
            get => (Point)GetValue(DashedLineEndProperty);
            set => SetValue(DashedLineEndProperty, value);
        }

        public string ArrowheadPoints
        {
            get => (string)GetValue(ArrowheadPointsProperty);
            set => SetValue(ArrowheadPointsProperty, value);
        }

        // 尺寸变化处理
        private void ArrowButton_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            UpdateLinePositions();
        }

        private void UpdateLinePositions()
        {
            double width = ActualWidth;
            double height = ActualHeight;

            if (width == 0 || height == 0) return; // 避免初始零值

            // 更新左垂直线
            LeftLineStart = new Point(width * 0.1, height * 0.8);
            LeftLineEnd = new Point(width * 0.1, height * 0.2);

            // 更新右垂直线
            RightLineStart = new Point(width * 0.9, height * 0.2);
            RightLineEnd = new Point(width * 0.9, height * 0.8);

            // 更新虚线
            DashedLineStart = DashedLineDirection == "LeftToRight"
                ? new Point(width * 0.1, height * 0.2)
                : new Point(width * 0.9, height * 0.2);
            DashedLineEnd = DashedLineDirection == "LeftToRight"
                ? new Point(width * 0.9, height * 0.8)
                : new Point(width * 0.1, height * 0.8);

            // 更新箭头
            ArrowheadPoints = LineOrientation == "Vertical"
                ? $"{width * 0.9 - 5},{height * 0.2} {width * 0.9},{height * 0.2} {width * 0.9 - 5},{height * 0.2 + 10}"
                : $"{width * 0.9},{height * 0.2} {width * 0.9 - 5},{height * 0.2 - 5} {width * 0.9 - 5},{height * 0.2 + 5}";
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }
}